package com.biodiv.miracle.web.interceptor;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author: tianquan
 * @date: 2025-05-28  10:27
 * @description: 拦截器注册类
 */
public class InterceptorRegistry {

    private final List<InterceptorRegistration> registrations = new ArrayList<>();

    private static final Comparator<Object> INTERCEPTOR_ORDER_COMPARATOR;

    static {
        INTERCEPTOR_ORDER_COMPARATOR = (o1, o2) -> {
            int i1 = o1 instanceof InterceptorRegistration ? ((InterceptorRegistration) o1).getOrder() : 0;
            int i2 = o2 instanceof InterceptorRegistration ? ((InterceptorRegistration) o2).getOrder() : 0;
            return Integer.compare(i1, i2);
        };
    }

    public List<InterceptorRegistration> getInterceptors() {
       return registrations.stream().sorted(INTERCEPTOR_ORDER_COMPARATOR).toList();
    }

    public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) {
        InterceptorRegistration registration = new InterceptorRegistration(interceptor);
        registrations.add(registration);
        return registration;
    }



}
